﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RBAC.Administry.Account
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        #region page logic

        /// <summary>
        /// 载入数据
        /// </summary>
        private void LoadData()
        {
            RBAC.Account account = ParseRequest();

            if (account != null)
            {
                // 绑定账户信息
                this.account.Text = account.ID;
                this.display.Text = string.IsNullOrEmpty(account.Display) ? "无" : account.Display;

            }
            else
            {
                CloseInput();
                report.ReportFailure("请求的账户不存在!");
            }
        }
        /// <summary>
        /// 关闭用户输入
        /// </summary>
        private void CloseInput()
        {
            PageHelper.Script(this, "closeInput();", scriptUpdatePanel);
        }        
        /// <summary>
        /// 解析用户请求的账户
        /// </summary>
        private RBAC.Account ParseRequest()
        {
            string id = Request.QueryString["account"];
            if (!string.IsNullOrEmpty(id))
            {
                return RBAC.Account.Load(id);
            }
            return null;
        }

        private void Submit()
        {
            RBAC.Authentication service = new Authentication();
            service.SetAccount(Request.QueryString["account"]);

            int result = service.ChangePassword(current.Text, replace.Text);
            if (result == 1)
            {
                PageHelper.Script(this, "done();",scriptUpdatePanel);
            }
            else if(result == -2)
            {
                PageHelper.Script(this, "invalid();",scriptUpdatePanel);
            }
        }

        #endregion

        #region event processing

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        protected void submit_Click(object sender, EventArgs e)
        {
            Submit();
        }


        #endregion

    }
}
